package com.auto.survey.activity.monitoring.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.R
import com.auto.survey.activity.monitoring.entity.MonitoringStation
import com.auto.survey.util.Helper

class MontoringStationAdapter : Adapter<MontoringStationAdapter.MontoringStationAdapterViewHodler>(){

    private var mItems:List<MonitoringStation> = mutableListOf()
    private var mOnClickListener:OnItemClickListener?=null

    fun setItems(data:List<MonitoringStation>) {
        mItems = data
        notifyDataSetChanged()
    }

    fun setOnItemClick(clickListener:OnItemClickListener) {
        mOnClickListener = clickListener
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MontoringStationAdapterViewHodler {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_monroting_station, parent, false)
        return MontoringStationAdapterViewHodler(view)
    }

    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }

    override fun onBindViewHolder(holder: MontoringStationAdapterViewHodler, position: Int) {
        holder.station_name.text = mItems[position].stationName
        holder.staticon_createtime.text = mItems[position].createTime


        //x
        if (mItems[position].stationX == 0.0) {
            holder.station_x.text = ""
        } else {
            holder.station_x.text= Helper.formateDecimal(mItems[position].stationX).toString()
        }

        //y
        if (mItems[position].stationY == 0.0) {
            holder.station_y.text = ""
        } else {
            holder.station_y.text= Helper.formateDecimal(mItems[position].stationY).toString()
        }

        //z
        if (mItems[position].stationZ == 0.0) {
            holder.station_z.text = ""
        } else {
            holder.station_z.text= Helper.formateDecimal(mItems[position].stationZ).toString()
            Log.i("MontoringStationAdapter", "station_z:${mItems[position].stationZ}, format = ${Helper.formateDecimal(mItems[position].stationZ).toString()}")
        }

        holder.root.setOnClickListener {
            mOnClickListener?.onItemClick(mItems[position])
        }

        holder.detail.setOnClickListener {
            mOnClickListener?.onItemDetailClick(mItems[position])
        }
    }

    class MontoringStationAdapterViewHodler(itemView:View):ViewHolder(itemView) {
        val root = itemView.findViewById<ConstraintLayout>(R.id.cl_monroting_station)

        val detail = itemView.findViewById<TextView>(R.id.tv_monroting_station_detail)
        val station_name = itemView.findViewById<TextView>(R.id.tv_monroting_station_name)
        val staticon_createtime = itemView.findViewById<TextView>(R.id.tv_monroting_station_createTime)

        val station_x = itemView.findViewById<TextView>(R.id.tv_monroting_point_xn)
        val station_y = itemView.findViewById<TextView>(R.id.tv_monroting_point_ye)
        val station_z = itemView.findViewById<TextView>(R.id.tv_monroting_point_zh)
    }

    interface OnItemClickListener {
        fun onItemClick(station: MonitoringStation)

        fun onItemDetailClick(station: MonitoringStation)
    }
}